package com.zsrd.system.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsrd.core.response.ResponseSupport;
import com.zsrd.system.domain.*;
import com.zsrd.system.domain.SysPower;
import com.zsrd.system.service.SysPowerService;
import com.zsrd.system.service.SysRolePowerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/powerManage")
public class SysPowerController {

    @Autowired
    private SysPowerService sysPowerService;

    @Autowired
    SysRolePowerService sysRolePowerService;

    /**
     * 分页
     * 传 menuId code 分页数据
     * 根据时间 order排序
     * 模糊查询
     * @param
     * @return
     */
//    @SaCheckPermission("admin")
    @RequestMapping(value = "query", method = RequestMethod.POST)
    public ResponseSupport query(@RequestBody SysPower sysPower) {
        if (sysPower.getMenuId() == 0 && sysPower.getParentMenuId() == 0){
            // 不传MenuId 根目录时全查
            IPage<SysPower> sysPower1 = sysPowerService.listAll(sysPower);
            return ResponseSupport.success(sysPower1);
        }else {
            IPage<SysPower> sysPowers = sysPowerService.listByPage(sysPower);
            return ResponseSupport.success(sysPowers);
        }
    }

    /**
     * 新增或者修改
     * @param 
     * @return
     */
    @Transactional
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public ResponseSupport saveOrUpdate(@RequestBody SysPower sysPower) {
        // 数据库中id = 0 新增
        long id1 = sysPower.getId();
        String id = String.valueOf(id1);
        if (StrUtil.equals(id, "0")) {
            // 新增
            sysPower.setCreateTime(new Date());
            if (sysPower.getMenuId() == 0){
                return ResponseSupport.fail("请选择菜单");
            }
            sysPowerService.save(sysPower);
            // 新增权限对应的角色 可以传入多个
            ArrayList<SysRolePower> list = new ArrayList<>();
            String[] split = sysPower.getRoleId().split(",");
            for (String s : split) {
                SysRolePower sysRolePower = new SysRolePower();
                sysRolePower.setRoleId(Integer.parseInt(s));
                // 去数据库里找新增的编码对应的id
                SysPower sysPower1 = sysPowerService.selectByCode(sysPower.getPowerCode());
                long powerId = sysPower1.getId();
                sysRolePower.setPowerId((int) powerId);
                list.add(sysRolePower);
            }
            sysRolePowerService.saveBatch(list);
            return ResponseSupport.success("成功");
        } else {
            //修改
            SysPower b = sysPowerService.getById(id);
            if (b == null) {
                return ResponseSupport.fail("修改失败");
            }
            // 修改权限信息
            sysPowerService.updateById(sysPower);
            // 修改中间表信息  先删除 再添加
            LambdaQueryWrapper<SysRolePower> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysRolePower::getRoleId, sysPower.getId());
            sysRolePowerService.remove(wrapper);
            // 添加
            ArrayList<SysRolePower> list1 = new ArrayList<>();
            String[] split = sysPower.getRoleId().split(",");
            for (String s : split) {
                SysRolePower sysRolePower = new SysRolePower();
                sysRolePower.setRoleId(Integer.parseInt(s));
                sysRolePower.setPowerId(Integer.parseInt(id));
                list1.add(sysRolePower);
            }
            boolean b1 = sysRolePowerService.saveBatch(list1);
            return ResponseSupport.success("成功");
        }
    }


    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public ResponseSupport deletePower(@RequestBody SysPower sysPower) {
        boolean b = sysPowerService.removeById(sysPower.getId());
        return ResponseSupport.success(b);
    }
}









